﻿using System;
using static GlobalField;
public class GlobalField_Write 
{
	 static int GetBaseValueTypeSize(string typeName) 
	 { 
		 switch(typeName){ 
		 case "System.Int16":  
			 return 2; 
		 case "System.Int32": 
 			 return 4; 
		 case "System.Int64": 
 			 return 8; 
		 case "System.UInt16": 
 			 return 2; 
		 case "System.UInt32": 
 			 return 4; 
		 case "System.UInt64": 
 			 return 8; 
		 case "System.Boolean": 
 			 return 4; 
		 case "System.Single": 
 			 return 4; 
		 case "System.Double": 
 			 return 8; 
		 case "System.Byte": 
 			 return 1; 
		 case "System.SByte": 
 			 return 1; 
		 default: 
 			 return 0; 
		 }
	}

	 public static byte[] Bytes_tagDataDescribe(tagDataDescribe obj)
	 {
		 // Marshal.SizeOf(tagDataDescribe)=4,Pack=2
		 byte[] bytes = new byte[4];
		 int sizeConst = 0; 
		 int index = 0; 
		 Array.Copy( BitConverter.GetBytes(obj.wDataSize) , 0, bytes, 0, 2);
		 Array.Copy( BitConverter.GetBytes(obj.wDataDescribe) , 0, bytes, 2, 2);
		 return bytes;
	 }

	 public static byte[] Bytes_ByteArray(Byte[] array, int sizeConst) 
	 { 
		 int index = 0;
		 int bitSize = 1; 
		 byte[] bytes = new byte[bitSize*sizeConst]; 
		 for (int i=0;i<sizeConst; i++)		 {
			 byte[] bts = BitConverter.GetBytes(array[i]); 
			 Array.Copy(bts, 0, bytes, index , bitSize); 
			 index += bitSize; 
		 }
		 return bytes; 
	 }
	 public static byte[] Bytes_IPC_Head(IPC_Head obj)
	 {
		 // Marshal.SizeOf(IPC_Head)=56,Pack=8
		 byte[] bytes = new byte[56];
		 int sizeConst = 0; 
		 int index = 0; 
		 byte[] bytes_desc = Bytes_tagDataDescribe(obj.desc);
		 Array.Copy(bytes_desc,0, bytes, 0, 4);
		 Array.Copy( BitConverter.GetBytes(obj.wVersion) , 0, bytes, 4, 2);
		 Array.Copy( BitConverter.GetBytes(obj.wRoomID) , 0, bytes, 6, 2);
		 Array.Copy( BitConverter.GetBytes(obj.wDataSize) , 0, bytes, 8, 2);
		 Array.Copy( BitConverter.GetBytes(obj.wMainCmdID) , 0, bytes, 12, 4);
		 Array.Copy( BitConverter.GetBytes(obj.wSubCmdID) , 0, bytes, 16, 8);
		 sizeConst = 32;
		 index = 24;
		 byte[] bts_szTypeName = Bytes_ByteArray(obj.szTypeName,32);
		 Array.Copy( bts_szTypeName , 0, bytes, 24, 32);
		 return bytes;
	 }

}
